package com.huluobo.a2207monthdemo.sign

import android.util.Log
import com.baidu.location.BDAbstractLocationListener
import com.baidu.location.BDLocation
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption
import com.baidu.mapapi.map.BaiduMap
import com.baidu.mapapi.map.MapStatusUpdateFactory
import com.baidu.mapapi.map.MapView
import com.baidu.mapapi.map.MyLocationData
import com.baidu.mapapi.model.LatLng
import com.huluobo.a2207monthdemo.databinding.ActivitySignBinding
import com.huluobo.base.mvvm.BaseActivity
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class SignActivity : BaseActivity<ActivitySignBinding, SignViewModel>() {
    private lateinit var mMapView: MapView
    private lateinit var baiduMap: BaiduMap
    override fun initData() {
    }

    override fun initView() {
        mMapView = binding.signMapView
        baiduMap = mMapView.map
        baiduMap.isMyLocationEnabled = true;
        LocationClient.setAgreePrivacy(true)

        //定位初始化
        //定位初始化
        val mLocationClient = LocationClient(applicationContext)

//通过LocationClientOption设置LocationClient相关参数

//通过LocationClientOption设置LocationClient相关参数
        val option = LocationClientOption()
        option.isOpenGps = true // 打开gps

        option.setCoorType("bd09ll") // 设置坐标类型

        option.setScanSpan(1000)

//设置locationClientOption

//设置locationClientOption
        mLocationClient.locOption = option

//注册LocationListener监听器

//注册LocationListener监听器
        val myLocationListener = MyLocationListener()
        mLocationClient.registerLocationListener(myLocationListener)
//开启地图定位图层
//开启地图定位图层
        mLocationClient.start()
    }

    inner class MyLocationListener : BDAbstractLocationListener() {
        override fun onReceiveLocation(location: BDLocation) {
            //mapView 销毁后不在处理新接收的位置
            if (location == null || mMapView == null) {
                return
            }

            val latitude = location.latitude
            val longitude = location.longitude
            Log.i("TagA", "经度:$latitude,维度:$longitude")
            //定位蓝点额外加的
            var ll = LatLng(latitude, longitude);
            val update = MapStatusUpdateFactory.newLatLngZoom(ll, 15f);
            baiduMap.animateMapStatus(update);

            val locData = MyLocationData.Builder()
                .accuracy(location.radius) // 此处设置开发者获取到的方向信息，顺时针0-360
                .direction(location.direction).latitude(location.latitude)
                .longitude(location.longitude).build()
            baiduMap.setMyLocationData(locData)
        }
    }

    override fun onResume() {
        super.onResume()
        mMapView.onResume()
    }

    override fun onPause() {
        super.onPause()
        mMapView.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        baiduMap.isMyLocationEnabled = false
        mMapView.onDestroy()
    }
}